﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;

using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.ComponentModel;

namespace IOModule
{
    [XmlInclude(typeof(IOSetting))]
    public class IOSetting
    {
        public BindingList<IOInputItem> listInputSettings;
        [XmlIgnore]
        public Dictionary<string, IOInputItem> dicInputSettings;

        public BindingList<IOOutputItem> listOutputSettings;
        [XmlIgnore]
        public Dictionary<string, IOOutputItem> dicOutputSettings;

        public BindingList<IOInputItem> listStandInputs;
        [XmlIgnore]
        public Dictionary<string, IOInputItem> dicStandInputs;

        public BindingList<IOOutputItem> listStandOutputs;
        [XmlIgnore]
        public Dictionary<string, IOOutputItem> dicStandOutputs;

        public bool bAutoResetLightSenser = false;
        public IOSetting()
        {
            listInputSettings = new BindingList<IOInputItem>();
            dicInputSettings = new Dictionary<string, IOInputItem>();
            listOutputSettings = new BindingList<IOOutputItem>();
            dicOutputSettings = new Dictionary<string, IOOutputItem>();
            listStandInputs = new BindingList<IOInputItem>();
            listStandOutputs = new BindingList<IOOutputItem>();
           

        }
        public static IOSetting Load()
        {
            IOSetting pDoc=null;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(IOSetting));
            FileStream fsReader = null;
            try
            {
                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/IOSetting" + ".xml");
                pDoc = (IOSetting)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
                pDoc.dicInputSettings = pDoc.listInputSettings.ToDictionary(p => p.strName);
                pDoc.dicOutputSettings = pDoc.listOutputSettings.ToDictionary(p => p.strName);
                pDoc.dicStandInputs = pDoc.listStandInputs.ToDictionary(p => p.strName);
                pDoc.dicStandOutputs = pDoc.listStandOutputs.ToDictionary(p => p.strName);
                Array arrageStandInputs=Enum.GetValues(typeof(StandInputName));
                int iStandInputs = arrageStandInputs.Length;
                int iCurrentStandardInputs = pDoc.listStandInputs.Count;
                if (iCurrentStandardInputs != iStandInputs)
                {
                    pDoc.listStandInputs.Clear();
                    foreach (StandInputName name in Enum.GetValues(typeof(StandInputName)))
                    {
                        IOInputItem item = new IOInputItem();
                        item.strName = name.ToString();
                        pDoc.listStandInputs.Add(item);
                    }
                    pDoc.dicStandInputs = pDoc.listStandInputs.ToDictionary(p => p.strName);
                }
                Array arrageStandOutput = Enum.GetValues(typeof(StandOutPutName));
                int iStandOutPuts = arrageStandOutput.Length;
                int iCurrentStandardOutputs = pDoc.listStandOutputs.Count;
                if (iCurrentStandardOutputs != iStandOutPuts)
                {
                    pDoc.listStandOutputs.Clear();
                    foreach (StandOutPutName name in Enum.GetValues(typeof(StandOutPutName)))
                    {
                        IOOutputItem item = new IOOutputItem();
                        item.strName = name.ToString();
                        pDoc.listStandOutputs.Add(item);
                    }
                    pDoc.dicStandOutputs = pDoc.listStandOutputs.ToDictionary(p => p.strName);
                }
            }
            catch(IOException)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new IOSetting();
                foreach (StandInputName name in Enum.GetValues(typeof(StandInputName)))
                {
                    IOInputItem item = new IOInputItem();
                    item.strName = name.ToString();
                    pDoc.listStandInputs.Add(item);
                    //if(name==StandInputName.Door|| name == StandInputName.Door1|| name == StandInputName.Door2||
                    //    name == StandInputName.Door3||name == StandInputName.EStop|| name == StandInputName.LightSenser
                    //        || name == StandInputName.LinghtSenser1)
                    if (name == StandInputName.Door || name == StandInputName.EStop)
                    {
                        item.bIgnore = true;
                    }
                }
                pDoc.dicStandInputs = pDoc.listStandInputs.ToDictionary(p => p.strName);
                foreach (StandOutPutName name in Enum.GetValues(typeof(StandOutPutName)))
                {
                    IOOutputItem item = new IOOutputItem();
                    item.strName = name.ToString();
                    pDoc.listStandOutputs.Add(item);
                }
                pDoc.dicStandOutputs = pDoc.listStandOutputs.ToDictionary(p => p.strName);
            }
            catch (Exception eMy)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }

                MessageBox.Show(eMy.ToString() + "即将打开备份文件", Application.StartupPath + @"/MuduleParameter/IOSetting" + ".xml");

                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/IOSetting" + ".xml.bak");
                pDoc = (IOSetting)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
                pDoc.dicInputSettings = pDoc.listInputSettings.ToDictionary(p => p.strName);
                pDoc.dicOutputSettings = pDoc.listOutputSettings.ToDictionary(p => p.strName);
                pDoc.dicStandInputs = pDoc.listStandInputs.ToDictionary(p => p.strName);
                pDoc.dicStandOutputs = pDoc.listStandOutputs.ToDictionary(p => p.strName);
                Array arrageStandInputs = Enum.GetValues(typeof(StandInputName));
                int iStandInputs = arrageStandInputs.Length;
                int iCurrentStandardInputs = pDoc.listStandInputs.Count;
                if (iCurrentStandardInputs != iStandInputs)
                {
                    pDoc.listStandInputs.Clear();
                    foreach (StandInputName name in Enum.GetValues(typeof(StandInputName)))
                    {
                        IOInputItem item = new IOInputItem();
                        item.strName = name.ToString();
                        pDoc.listStandInputs.Add(item);
                    }
                    pDoc.dicStandInputs = pDoc.listStandInputs.ToDictionary(p => p.strName);
                }
                Array arrageStandOutput = Enum.GetValues(typeof(StandOutPutName));
                int iStandOutPuts = arrageStandOutput.Length;
                int iCurrentStandardOutputs = pDoc.listStandOutputs.Count;
                if (iCurrentStandardOutputs != iStandOutPuts)
                {
                    pDoc.listStandOutputs.Clear();
                    foreach (StandOutPutName name in Enum.GetValues(typeof(StandOutPutName)))
                    {
                        IOOutputItem item = new IOOutputItem();
                        item.strName = name.ToString();
                        pDoc.listStandOutputs.Add(item);
                    }
                    pDoc.dicStandOutputs = pDoc.listStandOutputs.ToDictionary(p => p.strName);
                }

                pDoc.Save();
            }
            return pDoc;
        }
        public bool Save()
        {
            if (!Directory.Exists(Application.StartupPath + @"/MuduleParameter/"))
            {
                Directory.CreateDirectory(Application.StartupPath + @"/MuduleParameter/");
            }
            FileStream fsWriter = new FileStream(Application.StartupPath + @"/MuduleParameter/IOSetting" + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(IOSetting));
            xmlSerializer.Serialize(fsWriter, this);
            fsWriter.Flush(true);
            fsWriter.Close();

            File.Copy(Application.StartupPath + @"/MuduleParameter/IOSetting" + ".xml", Application.StartupPath + @"/MuduleParameter/IOSetting" + ".xml.bak", true);
            //FileStream fsWriter1 = new FileStream(WorldGeneral.AppParam.AppParamPath + @"/MuduleParameter/IOSetting" + ".xml.bak", FileMode.Create, FileAccess.Write, FileShare.Read);
            //XmlSerializer xmlSerializer1 = new XmlSerializer(typeof(IOSetting));
            //xmlSerializer1.Serialize(fsWriter1, this);
            //fsWriter1.Close();

            return true;
        }
    }
}
